﻿/***************************************************
 * File name: ItemController.cs 
 * Author: Team A
 * Change Log: Please see SVN repository: http://test-project-drew1.googlecode.com/svn/trunk/
 * Date: December 2013
 * Description: See <summary> tab below
 * *************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WSCeCommerce.Domain;
using WSCeCommerce.Helpers;
using WSCeCommerceWeb.Helpers;
using WSCeCommerceWeb.Models;

namespace WSCeCommerceWeb.Controllers
{

    /// <summary>
    /// Controller used to manage items (Generated by MS and customized by Team A)
    /// </summary>
    public class ItemController : Controller
    {
        // Customization: Renamed to be consistant with naming standards
        private WSCeCommerceDBContext _db = new WSCeCommerceDBContext();

        //
        // GET: /Item/

        public ActionResult Index()
        {
            return View(_db.Items.ToList());
        }

        //
        // GET: /Item/Details/5

        public ActionResult Details(int id = 0)
        {
            Item item = _db.Items.Find(id);
            if (item == null)
            {
                return HttpNotFound();
            }
            return View(item);
        }

        //
        // GET: /Item/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Item/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Item item)
        {
            if (ModelState.IsValid)
            {
                _db.Items.Add(item);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(item);
        }

        //
        // GET: /Item/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Item item = _db.Items.Find(id);
            if (item == null)
            {
                return HttpNotFound();
            }
            return View(item);
        }

        //
        // POST: /Item/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Item item)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(item).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(item);
        }

        //
        // GET: /Item/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Item item = _db.Items.Find(id);
            if (item == null)
            {
                return HttpNotFound();
            }
            return View(item);
        }

        //
        // POST: /Item/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Item item = _db.Items.Find(id);
            _db.Items.Remove(item);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }


        //Begin Customization: Added Search functionality.

        // Post: /Item/Search
        [HttpPost]
        public ActionResult Search(SearchCriteria criteria)
        {
            // Create a query for items
            IQueryable<Item> query = _db.Items.Select(item => item);

            //Add the text critera
            if (!String.IsNullOrWhiteSpace(criteria.Text))
            {
                query = query.Where(item => item.Name.Contains(criteria.Text));
            }

            if (criteria.Category != ListHelper.SEARCH_ALL_CATEGORIES)
            {
                     // Do an intersect between the desired categories and the categories for each item.
                    query = query.Where(item => (item.Categories.Any(cat => (cat.CategoryId == criteria.Category))));
            }

            // Run the query and return a result list
            List<Item> result = query.ToList();

            ViewBag.SearchCriteria = criteria;

            //Display the results
            return View("Index", result);
           

        }

        public ActionResult RemoveCategory(int itemId, int categoryId = 0)
        {
            Category category = _db.Categories.Find(categoryId);
            Item item = _db.Items.Find(itemId);

            if (category != null && item != null)
            {
                item.Categories.Remove(category);
                _db.SaveChanges();
                return new RedirectResult("~/Item/Edit/" + itemId);
            }

            return new HttpNotFoundResult();
           

        }
        // End Customization
       
        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}